<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>文章管理</el-breadcrumb-item>
      <el-breadcrumb-item>文章列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <!-- 搜索区域 -->
      <el-row :gutter="20">
        <el-col :span="7">
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            clearable
            @clear="getBlogList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getBlogList"
            ></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!-- 列表 -->
      <el-table :data="blogList" stripe border>
        <el-table-column type="index"></el-table-column>
        <el-table-column label="文章标题" prop="title"></el-table-column>
        <el-table-column
          label="类型"
          prop="type.name"
          width="100"
        ></el-table-column>
        <el-table-column label="发布时间" width="180">
          <template slot-scope="scope">
            {{ scope.row.createTime | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column label="更新时间" width="180">
          <template slot-scope="scope">
            {{ scope.row.updateTime | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column label="标签" width="350">
          <template slot-scope="scope">
            <el-tag v-for="(item, i) in scope.row.tags" :key="i">{{
              item.name
            }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="warning"
              icon="el-icon-edit"
              @click="editBlog(scope.row.id)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              icon="el-icon-delete"
              @click="deleteBlog(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      queryInfo: {
        query: "",
        pagenum: 1,
        pagesize: 5,
      },
      blogList: [],
      total: 0,
    };
  },
  mounted() {
    this.getBlogList();
  },
  methods: {
    async getBlogList() {
      const { data: res } = await this.$http.get("blogByQuery", {
        params: this.queryInfo,
      });
      if (res.meta.status !== 0) {
        return this.$message.error("获取文章列表失败");
      }
      this.blogList = res.data.blogs;
      this.total = res.data.total;
    },
    // 监听 pagesize 改变的事件
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize;
      this.getBlogList();
    },
    // 监听 页码值 改变的事件
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage;
      this.getBlogList();
    },
    editBlog(id){
        this.$router.push({path:'/blogEdit', query: {id: id}})
    },
    async deleteBlog(id){
      const confirmResult = await this.$confirm(
        "此操作将永久删除该文章, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);
      if (confirmResult !== "confirm") {
        return this.$message.info("已取消删除");
      }
      const { data: res } = await this.$http.delete("deleteBlog/" + id)

      if (res.meta.status === 0) {
        this.$message.success("删除文章成功！");
      }
      this.getBlogList();
    }
  },
};
</script>

<style lang="less" scoped>
</style>